package com.s2.m.btchat;

import java.io.IOException;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Samik
 */
public class Settings implements CommandListener {

    Display display;
    public static final String SERVICE_AT_STARTUP = "service";
    public static final String ENABLE_SOUNDS = "sound";
    private static boolean modified;
    /**
     * Keep the parent reference
     */
    private BTChat parent;
    /**
     * A form to display the settings items
     */
    private Form settingsForm = new Form("Settings");
    /**
     * checkboxes to enable or disable features
     */
    private ChoiceGroup serviceChoiceGroup;
    private String[] serviceStartupOptions = {"Enabled", "Disabled"};
    private ChoiceGroup soundChoiceCroup;
    private String[] soundOptions = {"On", "Off"};
    /**
     * Keep the boolean value for 'enable chat service at application startup'
     */
    private static boolean chatServiceAtStartUp = true;
    /**
     * Keep the boolean value for 'enable sounds'
     */
    private static boolean soundsEnabled = false;
    /**
     * Back command
     */
    Command cancelCommand = new Command("Cancel", Command.CANCEL, 0);
    /**
     * Update Command
     */
    Command updateCommand = new Command("Update", Command.SCREEN, 0);
    Image[] tickCross = new Image[2];

    public Settings(BTChat parent) {
        this.parent = parent;
        display = Display.getDisplay(parent);
        int h = display.getBestImageHeight(Display.CHOICE_GROUP_ELEMENT);
        String dim = (h >= 16) ? "16x16" : "12x12";
        try {
            tickCross[0] = Image.createImage("/com/s2/m/res/img/accept" + dim + ".png");
            tickCross[1] = Image.createImage("/com/s2/m/res/img/cross" + dim + ".png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        serviceChoiceGroup = new ChoiceGroup("Chat Service at App. Startup",
                ChoiceGroup.POPUP, serviceStartupOptions, tickCross);
        serviceChoiceGroup.setSelectedIndex(chatServiceAtStartUp ? 0 : 1, true);

        soundChoiceCroup = new ChoiceGroup("Sounds (not implemented yet)",
                ChoiceGroup.POPUP, soundOptions, tickCross);
        soundChoiceCroup.setSelectedIndex(soundsEnabled ? 0 : 1, true);

        settingsForm.append(serviceChoiceGroup);
        settingsForm.append(soundChoiceCroup);
        settingsForm.addCommand(cancelCommand);
        settingsForm.addCommand(updateCommand);

        settingsForm.setCommandListener(this);
    }

    public void showUI() {
        Display.getDisplay(parent).setCurrent(settingsForm);
    }

    public static boolean isChatServiceAtStartUp() {
        return chatServiceAtStartUp;
    }

    public static boolean isSoundsEnabled() {
        return soundsEnabled;
    }

    public static void setChatServiceAtStartUp(boolean chatServiceAtStartUp) {
        Settings.chatServiceAtStartUp = chatServiceAtStartUp;
    }

    public static void setSoundsEnabled(boolean soundsEnabled) {
        Settings.soundsEnabled = soundsEnabled;
    }

    public static boolean isModified() {
        return modified;
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cancelCommand) {
            parent.showUI();
        } else if (c == updateCommand) {
            chatServiceAtStartUp =
                    serviceChoiceGroup.getSelectedIndex() == 0 ? true : false;
            soundsEnabled =
                    soundChoiceCroup.getSelectedIndex() == 0 ? true : false;
            modified = true;
            parent.showUI();
        }
    }
}
